/* 
 * File:   main.cpp
 * Author: brady
 *
 * Created on June 6, 2011, 11:10 PM
 */

#include <core/Raytracer.h>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <sys/utime.h>
#include <sys/types.h>
#include <parsing/TestSceneBuilder.h>
#include <dto/RenderParams.h>
#include <parsing/XMLSceneBuilder.h>
#include <core/StochasticRNG.h>
#include <parsing/ArgumentParser.h>
#include <acceleration/VoxelGrid.h>
#include <core/PNGImage.h>
using namespace std;
using namespace cs5721;
//using namespace cs5721;

/*
 * 
 */


int main(int argc, char** argv) {
    //parse program args
    RenderArgs args = ArgumentParser::parseArgs(argc, argv);
    //allocate an image buffer and zero it
    float* data = new float[args.xResolution*args.yResolution*3];
    bzero(data, args.xResolution*args.yResolution*3);
    //create the scene from file
    gParams.acceleration = args.acceleration;
    XMLSceneBuilder xsb;
    cout << "Building scene... ";
    cout.flush();
    Scene* scene = xsb.buildSceneFromFile(args.inputFile.c_str());
    cout << "done\n";

    //copy parameters
    gParams.adaptiveMultiSampling = args.adaptiveMultiSampling;
    gParams.imageData = data;
    gParams.initialSamples = args.initialSamples;
    gParams.maxSamples = args.maxSamples;
    gParams.scene = scene;
    gParams.threadCount = args.threadCount;
    gParams.xResolution = args.xResolution;
    gParams.yResolution = args.yResolution;
    gParams.recursionLimit = args.recursionLimit;
    
    time_t tm = time(NULL);
    Raytracer::render();
    time_t tme = time(NULL);
    double td = tme - tm;

    cout <<"rendering time: " << td << " seconds" <<  endl;
	PNGImage pi;
	pi.writeFileData(args.outputFile, gParams.xResolution, gParams.yResolution, gParams.imageData); 
    return 0;
}
